import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";

@Injectable({
  providedIn: 'root'
})
export class DaData {
  result?: any
  url = "http://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address"
  token = "fc4e8b7eb51ed3b241ee3f896dd2107c52df1405"
  query = 'москва хабар'

  options = {
    method: "POST",
    mode: "cors",
    headers: {
      "Content-Type": "application/json",
      "Accept": "application/json",
      "Authorization": "Token " + this.token
    },
    body: JSON.stringify({query: this.query})
  }

  constructor(private httpClient: HttpClient,
              // private suggestService: DadataSuggest
  ) {
  }

  onInit(){

    console.log(this.token)
    console.log(this.query)
    console.log(this.options)
    // return this.httpClient.post(this.url, this.options,)
    //   .subscribe(response => {
    //     this.result = response
    //     console.log(this.result)
    //   })
    // // @ts-ignore
    // @ts-ignore
    fetch(this.url, this.options)
      .then(response => response.text())
      .then(result => console.log(result))
      .then(result => this.result = result)
      .catch(error => console.log("error", error));
  }
  getSuggestions(){
    // document.querySelector(document).ready(function () {
    //   console.log("Hello")
    //   document.querySelector("#address").suggestions({
    //     token: "fc4e8b7eb51ed3b241ee3f896dd2107c52df1405",
    //     type: "ADDRESS",
    //     /* Вызывается, когда пользователь выбирает одну из подсказок */
    //     onSelect: function(suggestion) {
    //       console.log(suggestion);
    //     }
    //   });
  }
}
